package com.partners.controller;

import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.constant.ErrorMessageEnum;
import com.partners.constant.PermissionEnum;
import com.partners.dao.bo.CustomerDO;
import com.partners.dao.dto.CustomerOutputDTO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.CustomerService;
import com.partners.service.UploadFileService;
import com.partners.service.UserService;
import com.partners.utils.ApiResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Map;

/**
 * 客户管理
 * @author liangyongle
 * @date 2018-12-11
 */
@RequestMapping("customer")
@RestController
@Slf4j
public class CustomerController {


    @Resource
    private CustomerService customerService;

    @Resource
    private UploadFileService uploadFileService;

    @Resource
    private UserService userService;



    /**
     * 获取客户列表
     * @param paging Paging //分页数
     *        name String  //名字
     *        phone Integer  //电话号码
     *        returnType  Integer //是否按照月份显示
     *        uid Integer 按合伙人查询
     * @return List
     */
    @PostMapping("customer_list")
    public ApiResult getCustomerList(@BeanValid @RequestBody Paging paging,
                                     @SimpleParam("returnType") Integer returnType,
                                     @SimpleParam("name") String name,
                                     @SimpleParam("phone") BigInteger phone,
                                     @SimpleParam("uid") Integer uid,
                                     @SimpleParam("isall") Integer isall){
        if(uid == null){
            return  ApiResultUtil.failure(ErrorMessageEnum.UID_NOT_FOUND);
        }

        if(isall == null){
            return  ApiResultUtil.failure(ErrorMessageEnum.TYPE_IS_ERROR);
        }
        returnType =  returnType !=null?returnType:0;
        uid = isall==1?null:uid;
        Map<String,Object> list = customerService.getCustomerList(paging,returnType,name,phone,uid);
        return ApiResultUtil.ok(list);
    }

    /**
     * 获取客户详情
     * @param Id Integer
     * @return List
     */
    @PostMapping("customer_detail")
    public ApiResult getCustomerDetail(@SimpleParam("id") Integer Id){
        if(Id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        CustomerOutputDTO list = customerService.getCustomerDetail(Id);
        return ApiResultUtil.ok(list);
    }

    /**
     * 删除客户
     * @param Id Integer
     * @return List
     */
    @PostMapping("customer_delete")
    public ApiResult CustomerDelete(@SimpleParam("id") Integer Id){
        System.out.println(Id);
        if(Id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        Integer result = customerService.CustomerDelete(Id);
        return ApiResultUtil.ok(result);
    }


    /**
     * 编辑客户
     * @param customerDo CustomerDO
     * @return List
     */
    @PostMapping("customer_edit")
    public ApiResult editCsutomer(@RequestBody CustomerDO customerDo){
        if(customerDo.getUid() == null){
            throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
        }
        if(customerDo.getPhone() == null){
            throw BusinessException.of(ErrorMessageEnum.PHONE_NOT_FOUND);
        }
        if(customerDo.getName() == null){
            throw BusinessException.of(ErrorMessageEnum.NAME_NOT_FOUND);
        }
        Integer result = customerService.editCustomer(customerDo);
        return ApiResultUtil.ok(result);
    }


    /**
     * 编辑客户二维码
     * @param Id Integer
     * @return List
     */
    @PostMapping("customer_code")
    public ApiResult getCsutomerCode(@SimpleParam("id") Integer Id){
        String url = "http://lyl.natapp1.cc/customerPage/scan_code/5";
        String qrcode = uploadFileService.createQrcode(url);
        return ApiResultUtil.ok(qrcode);
    }


    /**
     * 选择已报备的业主列表
     * @param paging Paging
     *        name String
     *        phone integer
     * @return List
     */

    @PostMapping("select_customer")
    public ApiResult selectCustomerList(@BeanValid @RequestBody Paging paging,
                                 @SimpleParam("name") String name,
                                 @SimpleParam("phone") BigInteger phone) {
        Map<String, Object> list = customerService.selectCustomerList(paging, name, phone);
        return ApiResultUtil.ok(list);
    }


    /**
     * 获取优惠券信息
     * @param id Integer
     * @return List
     */
    @PostMapping("CouponDetail")
    public ApiResult getCouponDetail(@SimpleParam("id") Integer id){
        if(id ==null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        Map<String,Object> detail = customerService.getCustomerCouponDetail(id);
        return ApiResultUtil.ok(detail);
    }

    /**
     * 领取优惠券
     * @param id Integer
     * @return List
     */
    @PostMapping("getCoupon")
    public ApiResult getCustomerCoupon(@SimpleParam("id") Integer id){
        if(id ==null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        customerService.getCustomerCoupon(id);
        return ApiResultUtil.ok(0);
    }

    /**
     * 核销优惠券
     * @param id  Integer uid Integer
     * @return boolean
    * */
    @PostMapping("verificate_coupon")
    public ApiResult verifucateCoupon(@SimpleParam("id") Integer id,
                                      @SimpleParam("uid") Integer uid,
                                      @SimpleParam("sale_money") Float saleMoney){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        if(uid == null){
            throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
        }

        if(saleMoney == null){
            throw BusinessException.of(ErrorMessageEnum.SALEMONEY_NOT_FOUND);
        }

        //判断权限
        Integer code = PermissionEnum.VERIFICATION_COUPON.getCode();
        boolean permission = userService.hasPermission(uid, code);
        if(!permission){
         throw  BusinessException.of(ErrorMessageEnum.HANDLE_NOT_FOUNT);
        }
        customerService.verificateCoupon(id,uid,saleMoney);
        return ApiResultUtil.ok(0);
    }

    /**
     * 获取所有成交用户列表
     * @param uid  Integer uid Integer
     * @return boolean
     * */
    @PostMapping("getOrderCustomer")
    public ApiResult getOrderCustomer(@BeanValid @RequestBody Paging paging,
                                      @SimpleParam("uid") Integer uid){
        //判断权限
        Integer code = PermissionEnum.GET_ORDER_CUSTOMER.getCode();
        boolean permission = userService.hasPermission(uid, code);
        if(!permission){
            throw  BusinessException.of(ErrorMessageEnum.HANDLE_NOT_FOUNT);
        }
        Map<String,Object> list = customerService.getOrderCustomerList(uid,paging);
        return ApiResultUtil.ok(list);
    }

    /**
     * 获取客户记录
     * @Param id Integer id
     * @return List
     */
    @PostMapping("getCustomerLog")
    public ApiResult getOrderCustomerLog(@SimpleParam("id") Integer id,
                                    @SimpleParam("uid") Integer uid){
        Integer code = PermissionEnum.GET_ORDER_CUSTOMER.getCode();
        boolean permission = userService.hasPermission(uid, code);
        if(!permission){
            throw  BusinessException.of(ErrorMessageEnum.HANDLE_NOT_FOUNT);
        }
        Map<String,Object> list = customerService.getOrderCustomerLog(id);
        return ApiResultUtil.ok(list);

    }











}
